<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <TreeTools :node-data="company" root class="el-tree" @add-depts="addDeptsFn" />
        <el-tree

          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <TreeTools
              :node-data="data"
              @del-dept="getDepartment"
              @add-depts="addDeptsFn"
              @emit-depts="emitDeptsFn"
            />
          </template>

        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="addDept"
      :is-show="isShow"
      :node-data="nodeData"
      :depts-list="deptsList"
      @closeDialog="closeDialogFn"
      @add-depts="getDepartment"
    />
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import AddDept from './components/AddDept.vue'
import { reqGetDepartment } from '@/api/departments'
import { transList2TreeData } from '@/utils'

export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '江苏传智播客',
        manager: '负责人'
      },
      isShow: false,
      nodeData: {},
      deptsList: [],
      loading: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.loading = true

      const { data } = await reqGetDepartment()
      console.log(data, 54)
      this.company = {
        name: data.companyName,
        manager: '负责人',
        id: ''
      }
      console.log(transList2TreeData(data.depts, ''), 74)
      this.departs = transList2TreeData(data.depts, '')
      this.deptsList = data.depts
      this.company.id = ''
      console.log(this.deptsList, 81)

      this.loading = false
    },
    closeDialogFn(flag) {
      this.isShow = flag
    },
    addDeptsFn(nodeData) {
      this.isShow = true
      this.nodeData = nodeData
      // this.$nextTick(() => {
      //   this.$refs.addDept.getSimpleUserList()
      //   this.$refs.addDept.dialoginTitles = '添加子部门'
      // })
    },
    emitDeptsFn(nodeData) {
      this.isShow = true
      this.nodeData = nodeData
      this.$nextTick(() => {
        // this.$refs.addDept.dialoginTitle = '编辑部门'
        this.$refs.addDept.getDepartDetail()
      })
    }

  }

}

</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    /* // 小三角的样式, 去掉默认的小三角的旋转效果 */
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    /* // 有子节点 且未展开 + */
    .el-icon-caret-right::before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    /* // 有子节点 且已展开 - */
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    /* // 没有子节点 */
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
